//
//  HistoryIncidentViewController.m
//  Runone
//
//  Created by runone02 on 16/8/27.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "HistoryIncidentViewController.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "RequestGinsengModel.h"
#import "CurrentEvent.h"
#import "RequestHelper.h"
#import "IncidentCell.h"
#import "HistoryEventDeatilController.h"

#define PageSize  10
@interface HistoryIncidentViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@property(nonatomic,assign)NSUInteger page;//保存当前的页数
@end

static NSString *cellID = @"historyCell";
@implementation HistoryIncidentViewController

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor cyanColor];
    self.tableView.contentInset = UIEdgeInsetsMake(99, 0, 44, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([IncidentCell class]) bundle:nil] forCellReuseIdentifier:cellID];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.tableView.mj_header beginRefreshing];
    [self loadNewData];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

#pragma mark - 下拉刷新
- (void)loadNewData {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetAllHistoryEventModel=[[RequestGinsengModel alloc] init];
    GetAllHistoryEventModel.URL         = GetEventDataURL;
    GetAllHistoryEventModel.DataValue   = @"";
    GetAllHistoryEventModel.UpdatedTime = @"";
    GetAllHistoryEventModel.SystemCode  = @"GuangleHighway";
    GetAllHistoryEventModel.MethodName  = @"GetAllHistoryEvent";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, ((NSArray *)dataValueModel).count)];
        [self.dataArray insertObjects:((NSArray *)dataValueModel) atIndexes:indexSet];
        [self.tableView reloadData];
        self.tableView.mj_footer.hidden = NO;
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_header.hidden = YES;
    };
    [requestHelper RequestGinsengModel:GetAllHistoryEventModel];
}
#pragma mark - 上拉加载
- (void)loadMoreData {
    
    NSUInteger page = self.page + 1;
    NSString *pageStr = [NSString stringWithFormat:@"%lu",(unsigned long)page];
    NSString *pageSize = [NSString stringWithFormat:@"%d",PageSize];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL         = GetEventDataURL;
    getCurrentEventModel.DataValue   = @"";
    getCurrentEventModel.UpdatedTime = [self DataValueStr:pageStr andSizeString:pageSize];
    getCurrentEventModel.SystemCode  = @"GuangleHighway";
    getCurrentEventModel.MethodName  = @"GetAllHistoryEventByPage";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        [self.dataArray addObjectsFromArray: ((NSArray *)dataValueModel)];
        [self.tableView reloadData];
        self.tableView.mj_footer.hidden = YES;
        [self.tableView.mj_header endRefreshing];
        self.page = page;
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IncidentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    CurrentEvent *model = self.dataArray[indexPath.row];
    [cell setCellData:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 108;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HistoryEventDeatilController *vc = [[HistoryEventDeatilController alloc] init];
    vc.CEModel = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];


}

-(NSString *)DataValueStr:(NSString *)pageStr andSizeString:(NSString *)sizeStr
{
    NSString *valueStr =[NSString stringWithFormat:@"[{\"key\":\"PageIndex\",\"value\":\"%@\"},{\"key\":\"PageSize\",\"value\":\"%@\"}]",pageStr,sizeStr];
    return valueStr;
}



@end
